package com.a1anwang.okble.demo.base;

import com.a1anwang.okble.demo.APP;
import com.a1anwang.okble.demo.MyUtils;
import com.a1anwang.okble.demo.ResourceTable;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import java.util.Optional;

/**
 * Created by a1anwang.com on 2017/12/27.
 */

public abstract class BaseAbility extends Ability implements Component.ClickedListener, Component.TouchEventListener {

    protected Context mContext;
    protected DependentLayout mHeadLayout;
    protected Image mBtnLeft;
    protected Text mHeadLeftText;
    protected Image mBtnRight;
    protected Text mTitleText;
    protected Text mHeadRightText;
    protected DirectionalLayout layoutLeft;
    protected DirectionalLayout layoutRight;
    private boolean fullScreen = false;
    private APP application;
    private ScrollView mContentView;
    private Display displayAttributes;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        fullScreen = isFullScreen();
        setFullScreen(fullScreen);
        super.setUIContent(ResourceTable.Layout_ability_base);
        application = APP.getApp();
        mContext = this;
        initHeaderView();
        setContentLayout();
        beforeInitView();
        initView();
        afterInitView();
        setStatusBarColor(Color.getIntColor("#1E90FF"));
    }

    public APP getApplication() {
        return application;
    }

    public void setStatusBarColor(int color) {
        getWindow().setStatusBarColor(color);
    }

    protected void initHeaderView() {
        // 初始化公共头部
        mContentView = (ScrollView) findComponentById(ResourceTable.Id_layout_container);
        mHeadLayout = (DependentLayout) findComponentById(ResourceTable.Id_layout_head);
        mHeadLeftText = (Text) findComponentById(ResourceTable.Id_tv_left);
        mHeadRightText = (Text) findComponentById(ResourceTable.Id_text_right);
        mBtnLeft = (Image) findComponentById(ResourceTable.Id_btn_left);
        mBtnRight = (Image) findComponentById(ResourceTable.Id_btn_right);
        mTitleText = (Text) findComponentById(ResourceTable.Id_tv_title);
        layoutLeft = (DirectionalLayout) findComponentById(ResourceTable.Id_layout_left);
        layoutRight = (DirectionalLayout) findComponentById(ResourceTable.Id_layout_right);
    }


    /**
     * 是否全屏
     *
     * @param fullScreen boolean类型
     */
    public void setFullScreen(boolean fullScreen) {
        this.fullScreen = fullScreen;
        if (fullScreen) {
            getWindow().setWindowFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN, WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        }
    }

    /**
     * isFullScreen
     *
     * @return boolean
     */
    public abstract boolean isFullScreen();

    /**
     * 设置布局文件
     */
    public abstract void setContentLayout();

    /**
     * 在实例化控件之前的逻辑操作
     */
    public abstract void beforeInitView();

    /**
     * 实例化控件
     */
    public abstract void initView();


    /**
     * 实例化控件之后的操作
     */
    public abstract void afterInitView();

    /**
     * onClick方法的封装
     *
     * @param component 控件对象
     */
    public abstract void onClickEvent(Component component);


    @Override
    public void onClick(Component v) {
        if (!MyUtils.isFastClick()) {
            onClickEvent(v);
        }
    }

    /**
     * 获得屏幕的宽度
     *
     * @return int
     */
    public int getScreenWidth() {
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(this.getContext());
        if (display.isPresent()) {
            displayAttributes = display.get();
        }
        DisplayAttributes displayAttributesw = displayAttributes.getAttributes();
        return displayAttributesw.width;
    }

    /**
     * 获得屏幕的高度
     *
     * @return int
     */
    public int getScreenHeigh() {
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(this.getContext());
        if (display.isPresent()) {
            displayAttributes = display.get();
        }
        DisplayAttributes displayAttributesq = displayAttributes.getAttributes();
        return displayAttributesq.height;
    }

    @Override
    public void setUIContent(ComponentContainer componentContainer) {
        DirectionalLayout.LayoutConfig config = new DirectionalLayout
                .LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
        mContentView.addComponent(componentContainer, config);
    }

    /**
     * 设置头部是否可见
     *
     * @param visibility visibility
     */
    public void setHeadVisibility(int visibility) {
        mHeadLayout.setVisibility(visibility);
    }


    /**
     * 点击左按钮
     *
     * @param component 控件对象
     */
    public void onHeadLeftClick(Component component) {
        terminateAbility();
    }

    /**
     * 点击右按钮
     *
     * @param component 控件对象
     */
    public void onHeadRightClick(Component component) {

    }

    /**
     * 是否可见
     *
     * @param visibility 值
     */
    public void setHeadRightVisibility(int visibility) {
        mHeadLayout.setVisibility(visibility);
    }


    public Image getHeadRightButton() {
        return mBtnRight;
    }

    /**
     * 设置右文本
     *
     * @param text 字符串
     */
    public void setRightText(String text) {
        mHeadRightText.setText(text);
    }

    /**
     * 设置右文本
     *
     * @param resid 引入资源ID
     */
    public void setRightText(int resid) {
        mHeadRightText.setText(resid);
    }

    /**
     * 设置左文本
     *
     * @param text 字符串
     */
    public void setLeftText(String text) {
        mHeadLeftText.setText(text);
    }

    /**
     * 设置左文本
     *
     * @param resid 引入资源ID
     */
    public void setLeftText(int resid) {
        mHeadLeftText.setText(resid);
    }


    /**
     * 设置左边是否可见
     *
     * @param visibility 值
     */
    public void setHeadLeftButtonVisibility(int visibility) {
        mBtnLeft.setVisibility(visibility);
    }

    /**
     * 设置右边是否可见
     *
     * @param visibility visibility
     */
    public void setHeadRightButtonVisibility(int visibility) {
        mBtnRight.setVisibility(visibility);
    }

    /**
     * 设置标题
     *
     * @param titleId int类型
     */
    public void setTitle(int titleId) {
        setTitle(getString(titleId), false);
    }

    /**
     * 设置标题
     *
     * @param titleId      int类型
     * @param hideLeftIcon boolean类型
     */
    public void setTitle(int titleId, boolean hideLeftIcon) {
        setTitle(getString(titleId), hideLeftIcon);
    }

    /**
     * 设置标题
     *
     * @param title 字符串
     */
    public void setTitle(String title) {
        setTitle(title, false);
    }

    /**
     * 设置标题
     *
     * @param title        字符串
     * @param hideLeftIcon boolean类型
     */
    public void setTitle(String title, boolean hideLeftIcon) {
        mTitleText.setText(title);
        if (hideLeftIcon) {
            mBtnLeft.setVisibility(Component.HIDE);
        } else {
            mBtnLeft.setVisibility(Component.VISIBLE);
        }
    }

    /**
     * 设置标题
     *
     * @param bundleName  bundleName跳转是传值
     * @param abilityName abilityName用于传值
     */
    public void startAbility(String bundleName, String abilityName) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withBundleName(bundleName)
                .withAbilityName(abilityName)
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        return false;
    }
}
